import { SHOW_CONFIRM_DELETE_DIALOG, HIDE_CONFIRM_DELETE_DIALOG } from '../reducers/confirm-delete-dialog.js';
import { deletePath } from './files-actions.js';
import { loadDirectory } from './directory-actions.js';

/**
 * 显示提示框
 * @param  {string}     path
 * @param  {bool}       isDir
 * @return {Object}
 */
export function showConfirmDeleteDialog(path, isDir = false) {
    return {
        type: SHOW_CONFIRM_DELETE_DIALOG,
        payload: {
            path,
            isDir
        }
    };
}

/**
 * 关闭提示框
 * @param  {bool}    confirmed
 */
export function hideConfirmDeleteDialog(confirmed = false) {
    return (dispatch, getState) => {
        // 关闭输入框
        dispatch({
            type: HIDE_CONFIRM_DELETE_DIALOG
        });

        // 关闭输入框后是否继续执行其他 Action
        if (confirmed) {
            let { path } = getState().confirmDeleteDialog;

            dispatch(deletePath(path))
                .then(() => dispatch(loadDirectory()));
        }
    };
}